/**
* AutoSWF - Cody Cushing. March 26, 2009
* Copyright (c) 2009 Cody Cushing
* No use restrictions. Maintain this notice in all versions. 
**/

package com.autoswf.automator
{
	import flexunit.framework.TestCase;

	public class AutomationClientTest extends TestCase
	{
		private var descriptor1:XML = <node delay="2" className="flash.events::Event" type="change" path=""/>;
		private var descriptor2:XML = <node delay="2" className="flash.events::Event" type="complete" path=""/>;
		private var mockScript:XML = <root>{descriptor1}{descriptor2}</root>;
		
		private var ac:AutomationClient;
		private var cmd:MockRemoteLoadCommand;
		
		public function AutomationClientTest(methodName:String=null)
		{
			super(methodName);
		}
		
		override public function setUp():void
		{
			cmd = new MockRemoteLoadCommand();
			ac = new AutomationClient();
			ac.remoteLoadCommand = cmd;
		}
		
		/**
		 * Tests that observer calling notify will save it to testCase 
		 */		
		public function testNotify():void
		{
			ac.notify(descriptor1.toXMLString());
			assertContained("descriptor was not added to script", descriptor1.toString(), ac.testCase.toString());
		}
		
		/**
		 * Tests notify ignores descriptors additions if in record mode
		 */
		public function testNotifyInRecordMode():void
		{
			ac.recording = false;
			ac.notify(descriptor1.toXMLString());
			assertEquals("descriptor was not added to script", "<root/>", ac.testCase.toXMLString());
		}
		
		/**
		 * Tests the ability to set filters for event types. Expected to store all events of type or just those with types added 
		 * added to the filter array.  
		 */		
		public function testNotifyFilter():void
		{
			ac.eventTypes = ["change", "complete"];
			ac.notify(<node delay="2" className="flash.events::Event" type="close" path=""/>);
			assertNotContained("descriptor was not added to script", descriptor1.toXMLString(), ac.testCase.toXMLString());
		}
		
		/**
		 * Tests that a script can be loaded externally. 
		 */		
		public function testSetTestCase():void
		{
			ac.testCase = mockScript;
			assertEquals("failed to load script", mockScript.toXMLString(), ac.testCase.toXMLString())
		}
		
		/**
		 * Tests that descriptors in a script can be run in order, with delays. 
		 */		
		public function testRunTestCase():void
		{
			var handler1:Function = function():void
			{
				assertEquals("failed to run first descriptor node", descriptor1.toXMLString(), cmd.descriptor.toXMLString());
			};
			
			var handler2:Function = function():void
			{
				assertEquals("failed to run last descriptor node", descriptor2.toXMLString(), cmd.descriptor.toXMLString());
			};
			
			ac.testCase = mockScript;
			ac.addEventListener(AutomationEvent.DESCRIPTOR_REMOVED, addAsync(handler1, 3));
			ac.addEventListener(AutomationEvent.SCRIPT_FINISHED, addAsync(handler2, 6));
			ac.runTestCase();
		}
	
	}
}